/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jjsoft.celularessuc.gco.ui;


import com.jjsoft.celularessuc.gco.mdl.Cliente;
import com.jjsoft.celularessuc.gco.mdl.Deuda;
import com.jjsoft.celularessuc.gco.neg.ClienteManager;
import com.jjsoft.celularessuc.gco.neg.DeudaManager;
import com.jjsoft.celularessuc.gco.util.MiModelo;
import java.util.List;
import javax.swing.UIManager;
//import reportes.ReportePDF;

/**
 *
 * @author wilo
 */
public class VDeudor extends javax.swing.JFrame {

    /**
     * Creates new form VSeleccionArticulo
     */
    
    List<Deuda> listaDeudores;
    List<Cliente> listaCliente;
    
    Cliente cli=new Cliente();
    Deuda art = new Deuda();
    int tipoI=0;
    DeudaManager dm = new DeudaManager();
    ClienteManager cm = new ClienteManager();
    public VDeudor() {
        this.setIconImage(new javax.swing.ImageIcon(getClass().getResource("/img/1362798841_CellPhone.png")).getImage());
        initComponents();
        
    }
    public VDeudor(int valor) {
        initComponents();
        tipoI=valor;
        
        listaCliente = cm.listarClientes();
        listaDeudores=dm.listarDeudas();
        cargarDatos(listaDeudores);
        cmdSeleccionar.setText("Exportar PDF");

  
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        cmdTodo = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        dtgArticulos = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        txtMatch = new javax.swing.JTextField();
        cmdBuscar = new javax.swing.JButton();
        cmdSeleccionar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        cmdTodo.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        cmdTodo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/listar.png"))); // NOI18N
        cmdTodo.setText("    Mostrar Todos");
        cmdTodo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdTodoActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Ubuntu", 1, 18)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(0, 0, 153));
        jLabel2.setText("Deudas de los Clientes");

        dtgArticulos.setAutoCreateRowSorter(true);
        dtgArticulos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        dtgArticulos.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(dtgArticulos);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(51, 51, 51));
        jLabel1.setText("Buscar en la tabla:");

        txtMatch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtMatchActionPerformed(evt);
            }
        });
        txtMatch.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                txtMatchPropertyChange(evt);
            }
        });
        txtMatch.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtMatchKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtMatchKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtMatchKeyTyped(evt);
            }
        });

        cmdBuscar.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        cmdBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/buscar.png"))); // NOI18N
        cmdBuscar.setText("  Buscar");
        cmdBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdBuscarActionPerformed(evt);
            }
        });

        cmdSeleccionar.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        cmdSeleccionar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/pdf.png"))); // NOI18N
        cmdSeleccionar.setText("  Seleccionar");
        cmdSeleccionar.setEnabled(false);
        cmdSeleccionar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdSeleccionarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtMatch, javax.swing.GroupLayout.PREFERRED_SIZE, 249, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(cmdBuscar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(32, 32, 32)
                        .addComponent(cmdSeleccionar)
                        .addGap(148, 148, 148))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 724, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(12, 12, 12)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel2)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(cmdTodo)
                                        .addGap(296, 296, 296)))))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addGap(18, 18, 18)
                .addComponent(cmdTodo, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(9, 9, 9)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 230, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cmdSeleccionar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 39, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(txtMatch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(cmdBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(8, 8, 8))
        );

        setSize(new java.awt.Dimension(764, 426));
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void cmdTodoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdTodoActionPerformed
        // TODO add your handling code here:
        
            listaDeudores = dm.listarDeudas();
            tipoI=0;
            cargarDatos(listaDeudores);
            cmdSeleccionar.setEnabled(false);
     
    }//GEN-LAST:event_cmdTodoActionPerformed

    private void txtMatchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtMatchActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtMatchActionPerformed

    private void txtMatchPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_txtMatchPropertyChange
        // TODO add your handling code here:
    }//GEN-LAST:event_txtMatchPropertyChange

    private void txtMatchKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtMatchKeyPressed
        // TODO add your handling code here:
        if(evt.getKeyCode()==10)
        {
            cmdBuscar.doClick();
        }
    }//GEN-LAST:event_txtMatchKeyPressed

    private void txtMatchKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtMatchKeyReleased
        // TODO add your handling code here:
    }//GEN-LAST:event_txtMatchKeyReleased

    private void txtMatchKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtMatchKeyTyped
        // TODO add your handling code here:
    }//GEN-LAST:event_txtMatchKeyTyped

    private void cmdBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdBuscarActionPerformed
        // TODO add your handling code here:
        
        if (txtMatch.getText().length() > 0) {
            //listaCliente = controladorC.findHibernateSearch(cli, txtMatch.getText(), "nombre", "direccion", "ciudad","telefono","email");
            listaCliente = cm.listarClientes(txtMatch.getText());
            tipoI=0;
            cargarDatos(listaDeudores);
            listaCliente = cm.listarClientes();
        }
    }//GEN-LAST:event_cmdBuscarActionPerformed

    private void cmdSeleccionarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdSeleccionarActionPerformed
        // TODO add your handling code here:
        //ReportePDF reporte=new ReportePDF();
        //reporte.generarPDF("Deudores.jasper", "ListaDeudores.pdf");
        
    }//GEN-LAST:event_cmdSeleccionarActionPerformed

    private void cmdTodo1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdTodo1ActionPerformed
        // TODO add your handling code here:
        tipoI=2;
        cargarDatos(listaDeudores);
        cmdSeleccionar.setEnabled(false);
    }//GEN-LAST:event_cmdTodo1ActionPerformed

    private void cmdTodo2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdTodo2ActionPerformed
        // TODO add your handling code here:
        tipoI=1;
                cargarDatos(listaDeudores);
                cmdSeleccionar.setEnabled(true);
    }//GEN-LAST:event_cmdTodo2ActionPerformed

    /**
     * @param args the command line arguments
     */
    private void cargarDatos(List<Deuda> listaDeuda) {
        MiModelo modelo = new MiModelo();
        String[] identificadores = new String[]{"IdCliente", "Nombre", "Valor", "FechaVencimiento","Estado"};
        modelo.setColumnIdentifiers(identificadores);
        for (int i = 0; i < listaDeuda.size(); i++) 
        {
            if (tipoI==1)
            {
                if (listaDeuda.get(i).getEstado()==0) 
                {
                    for(int j=0; j<listaCliente.size(); j++)
                    {
                        if(listaCliente.get(j).getCedula().compareTo(listaDeuda.get(i).getCedulaCliente().getCedula())==0)
                        {
                            String[] articulo = new String[]{listaCliente.get(j).getCedula(),listaCliente.get(j).getApellidos()+" "+listaCliente.get(j).getNombres(),  listaDeuda.get(i).getValor()+"", listaDeuda.get(i).getFechaVencimiento()+"","Deuda"};
                            modelo.addRow(articulo);
                        }
                    }
                }
            }
            if (tipoI==2)
            {
                if (listaDeuda.get(i).getEstado()==1) 
                {
                    for(int j=0; j<listaCliente.size(); j++)
                    {
                        if(listaCliente.get(j).getCedula().compareTo(listaDeuda.get(i).getCedulaCliente().getCedula())==0)
                        {
                            String[] articulo = new String[]{listaCliente.get(j).getCedula(),listaCliente.get(j).getApellidos()+" "+listaCliente.get(j).getNombres(),  listaDeuda.get(i).getValor()+"", listaDeuda.get(i).getFechaVencimiento()+"","Pagado"};
                            modelo.addRow(articulo);
                        }
                    }
                }
            }
            if (tipoI==0)
            {
                if (listaDeuda.get(i).getEstado()==1) 
                {
                    for(int j=0; j<listaCliente.size(); j++)
                    {
                        if(listaCliente.get(j).getCedula().compareTo(listaDeuda.get(i).getCedulaCliente().getCedula())==0)
                        {
                            String[] articulo = new String[]{listaCliente.get(j).getCedula(),listaCliente.get(j).getApellidos()+" "+listaCliente.get(j).getNombres(),  listaDeuda.get(i).getValor()+"", listaDeuda.get(i).getFechaVencimiento()+"","Pagado"};
                            modelo.addRow(articulo);
                        }
                    }
                }
                if (listaDeuda.get(i).getEstado()==0) 
                {
                    for(int j=0; j<listaCliente.size(); j++)
                    {
                        if(listaCliente.get(j).getCedula().compareTo(listaDeuda.get(i).getCedulaCliente().getCedula())==0)
                        {
                            String[] articulo = new String[]{listaCliente.get(j).getCedula(),listaCliente.get(j).getApellidos()+" "+listaCliente.get(j).getNombres(),  listaDeuda.get(i).getValor()+"", listaDeuda.get(i).getFechaVencimiento()+"","Deuda"};
                            modelo.addRow(articulo);
                        }
                    }
                }
            }
        }
        dtgArticulos.setModel(modelo);
        dtgArticulos.getColumn("IdCliente").setPreferredWidth(60);
        dtgArticulos.getColumn("Nombre").setPreferredWidth(200);
        dtgArticulos.getColumn("Valor").setPreferredWidth(120);
        dtgArticulos.getColumn("FechaVencimiento").setPreferredWidth(120);
        dtgArticulos.getColumn("Estado").setPreferredWidth(120);
    }
    
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(VDeudor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(VDeudor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(VDeudor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(VDeudor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            System.out.println("Error setting native LAF: " + e);
        }
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new VDeudor().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cmdBuscar;
    private javax.swing.JButton cmdSeleccionar;
    private javax.swing.JButton cmdTodo;
    private javax.swing.JTable dtgArticulos;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField txtMatch;
    // End of variables declaration//GEN-END:variables
}
